package com.example.ibeacon_station;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Vibrator;
import android.app.AlertDialog;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

/**
 * Created by henry on 10/28/17.
 */

public class SystemService {
    private Context mContext;
    private static Vibrator sVibrator;
    private static TextView mDebugBar;
    public static AlertDialog sWaitDialog;

    public SystemService(Context context){
        mContext = context;
        MainActivity ma = (MainActivity) mContext;
        sVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
//        mDebugBar = (TextView) ma.findViewById(R.id.debug_bar);
//        mDebugBar.setLines(3);
//        mDebugBar.setMovementMethod(ScrollingMovementMethod.getInstance());
//        mDebugBar.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                vibrate(10);
//                mDebugBar.setText(null);
//                return true;
//            }
//        });
    }

    public static void vibrate(long time){
        sVibrator.vibrate(time);
    }

    public static void vibrate(){
        sVibrator.vibrate(50);
    }

    public static void debug(String tag,String info){
        if(mDebugBar.getText()!=null)mDebugBar.append("\n");
        mDebugBar.append(Html.fromHtml("<font color='#CB7832'>"+tag+"</font>&emsp;"+info));
        Log.i(tag,info);
        if(mDebugBar.getLineCount()>3)mDebugBar.scrollBy(0,mDebugBar.getLineHeight());
    }
    public static void debug(String info){
        if(mDebugBar.getText()!=null)mDebugBar.append("\n");
        mDebugBar.append(info+"\n");
        Log.i("DEBUG",info);
        if(mDebugBar.getLineCount()>3)mDebugBar.scrollBy(0,mDebugBar.getLineHeight());
    }

    public static void debugSwitch(){
        if(mDebugBar.getVisibility()==View.VISIBLE)mDebugBar.setVisibility(View.GONE);
        else mDebugBar.setVisibility(View.VISIBLE);
    }

    public static void fatalWarning(Context context,String title,String message){
        new AlertDialog.Builder(context)
//                .setIcon(R.drawable.ic_error_outline_black_24dp)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButton("退出应用", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                    }
                })
                .setCancelable(false)
                .create().show();
    }

    public static void warning(Context context,String title,String message){
        new AlertDialog.Builder(context)
//                .setIcon(R.drawable.ic_warning_black_24dp)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create().show();
    }

    public static void waitDialogShow(Context context, String msg){
        sWaitDialog = new AlertDialog.Builder(context)
                .setCancelable(false)
                .setMessage(msg)
                .create();
        sWaitDialog.show();
    }

    public static void waitDialogDismiss(){
        if(sWaitDialog!=null && sWaitDialog.isShowing())sWaitDialog.dismiss();
    }
}